home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 10333 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: kuhub.cc.ukans.edu!anh
  2. From: anh@kuhub.cc.ukans.edu
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Tradition or what?
  5. Message-ID: <1996Mar16.172511.116133@kuhub.cc.ukans.edu>
  6. Date: 16 Mar 96 17:25:11 CST
  7. References: <4g0elg$mdr@redstone.interpath.net> <4hpd8a$d70@alterdial.UU.NET> <314ac1ef.49220064@nntp.ix.netcom.com>
  8. Organization: University of Kansas Academic Computing Services
  9.  
  10. I agree.  The sizeof() just slipped my mind when I typed this in. :-)
  11.  
  12. Anh
  13.  
  14. In article <314ac1ef.49220064@nntp.ix.netcom.com>, miker3@ix.netcom.com (Mike Rubenstein) writes:
  15. > anh@kuhub.cc.ukans.edu wrote:
  16. >> Well, I found one good use of magic numbers such as when one needs a 
  17. >> localized temporary buffer of data.
  18. >> 
  19. >> int func()
  20. >> {
  21. >>     FILE *fp;
  22. >>     char buf[15+1];
  23. >>     
  24. >>     ...
  25. >> 
  26. >>     fgets(buf,15,fp);
  27. >> }
  28. >> 
  29. >> 
  30. >> Well, if I know the data is always going to be less than 15 or whatever, 
  31. >> there is really no need to use a #define here.
  32. > Non sequitur.  Whether you know that the data is going to be less than
  33. > 15 is immaterial.  In this situration I'd very rarely use a #define
  34. > for the size of the buffer, but I would absolutely never repeat the
  35. > size as a literal later in the code.  My preference is something like
  36. >     int func()
  37. >     {
  38. >       FILE *fp;
  39. >       char buf[15];
  40. >      
  41. >        ...
  42. >  
  43. >        fgets(buf, sizeof buf, fp);
  44. >     }
  45. > Note that I've changed the size of the buffer to 15 on the assumption
  46. > that the intention is that the size of the data be at most 14
  47. > characters excluding the terminating nul.
  48. > Michael M Rubenstein
  49.